package com.edfu.core.testmgt.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.edfu.core.commons.util.CategoryMgtBeanTransformer;
import com.edfu.core.testmgt.persistence.entity.AnswersEntity;
import com.edfu.core.testmgt.persistence.entity.FormatTypeEntity;
import com.edfu.core.testmgt.persistence.entity.OptionEntity;
import com.edfu.core.testmgt.persistence.entity.QuestionEntity;
import com.edfu.core.testmgt.persistence.entity.QuestionTypeEntity;
import com.edfu.core.testmgt.persistence.entity.TestBundleEntity;
import com.edfu.core.testmgt.persistence.entity.TestEntity;
import com.edfu.core.testmgt.persistence.entity.TestTypeEntity;
import com.edfu.core.testmgt.persistence.entity.UserTestSubscriptionEntity;
import com.edfu.core.testmgt.vo.FormatType;
import com.edfu.core.testmgt.vo.Option;
import com.edfu.core.testmgt.vo.Question;
import com.edfu.core.testmgt.vo.QuestionType;
import com.edfu.core.testmgt.vo.Test;
import com.edfu.core.testmgt.vo.TestBundle;
import com.edfu.core.testmgt.vo.TestType;
import com.edfu.core.testmgt.vo.UserTest;

public class TestMgtBeanTransformer {
	
	public static TestEntity transformTest(Test test) {
		TestEntity te = null;
		
		if (test != null) {
			te = new TestEntity();
			te.setActive(test.isActive());
			te.setAllowChangeAnswers(test.isAllowChangeAnswers());
			te.setAllowReattempt(test.isAllowReattempt());
			te.setAllowReview(test.isAllowReview());
			te.setCode(test.getCode());
			te.setCreatedBy(test.getCreatedBy());
			te.setCreatedDate(test.getCreatedDate());
			te.setDescription(test.getDesc());
			te.setDuration(test.getDuration());
			te.setId(test.getId());
			te.setInstructions(test.getInstructions());
			te.setLogo(test.getLogo());
			te.setName(test.getName());
			te.setNegativeMarks(test.getNegativeMarks());
			te.setPassMarks(test.getPassMarks());
			te.setPositiveMarks(test.getPositiveMarks());
//			te.setQuestions(transformQuestion(test.getQuestions()));
//			te.setTestBundle(transformTestBundle(test.getTestBundle()));
//			te.setTestCategory(transformTestCategory(test.getTestCategory()));
//			te.setTestType(transformTestType(test.getTestType()));
			te.setTimeBased(test.isTimeBased());
			te.setTotalMarks(test.getTotalMarks());	
		}
		
		return te;		
	}
	
	public static TestTypeEntity transformTestType(TestType testType) {
		TestTypeEntity testTypeEntity = null;
		
		if (testType != null) {
			testTypeEntity = new TestTypeEntity();
			testTypeEntity.setDescription(testType.getDesc());
			testTypeEntity.setName(testType.getName());
		}
		
		return testTypeEntity;
	}
	
	public static TestType transformTestType(TestTypeEntity te) {
		TestType testType = null;
		
		if (te != null) {
			testType = new TestType();
			testType.setDesc(te.getDescription());
			testType.setId(te.getId());
			testType.setName(te.getName());
		}
		return testType;
	}

	public static Test transformTest(TestEntity testEntity) {
		Test test = null;
		
		if (testEntity != null) {
			test = new Test();
			test.setActive(testEntity.isActive());
			test.setAllowChangeAnswers(testEntity.isAllowChangeAnswers());
			test.setAllowReattempt(testEntity.isAllowReattempt());
			test.setAllowReview(testEntity.isAllowReview());
			test.setCode(testEntity.getCode());
			test.setCreatedBy(testEntity.getCreatedBy());
			test.setCreatedDate(testEntity.getCreatedDate());
			test.setDesc(testEntity.getDescription());
			test.setDuration(testEntity.getDuration());
			test.setId(testEntity.getId());
			test.setInstructions(testEntity.getInstructions());
			test.setLogo(testEntity.getLogo());
			test.setName(testEntity.getName());
			test.setNegativeMarks(testEntity.getNegativeMarks());
			test.setPassMarks(testEntity.getPassMarks());
			test.setPositiveMarks(testEntity.getPositiveMarks());
			test.setQuestions(transformQuestion(testEntity.getQuestions()));
			test.setTestBundle(transformBasicTestBundleDetails(testEntity.getTestBundle()));
			test.setCategories(CategoryMgtBeanTransformer.transformCategory(testEntity.getCategories()));
			test.setTestType(transformTestType(testEntity.getTestType()));
			test.setTimeBased(testEntity.isTimeBased());
			test.setTotalMarks(testEntity.getTotalMarks());			
		}
		
		return test;
	}
	
	private static TestBundle transformBasicTestBundleDetails(TestBundleEntity testBundleEntity) {
		TestBundle testBundle = null;
		
		if (testBundleEntity != null) {
			testBundle = new TestBundle();
			testBundle.setCode(testBundleEntity.getCode());
			testBundle.setDesc(testBundleEntity.getDescription());
			testBundle.setId(testBundleEntity.getId());
			testBundle.setName(testBundleEntity.getName());
			testBundle.setPublisher(testBundleEntity.getPublisher());
			testBundle.setActive(testBundleEntity.isActive());						
		}
		
		return testBundle;
	}

	public static Test transformBasicTestDetails(TestEntity testEntity) {
		Test test = null;
		
		if (testEntity != null) {
			test = new Test();
			test.setActive(testEntity.isActive());
			test.setAllowChangeAnswers(testEntity.isAllowChangeAnswers());
			test.setAllowReattempt(testEntity.isAllowReattempt());
			test.setAllowReview(testEntity.isAllowReview());
			test.setCode(testEntity.getCode());
			test.setCreatedBy(testEntity.getCreatedBy());
			test.setCreatedDate(testEntity.getCreatedDate());
			test.setDesc(testEntity.getDescription());
			test.setDuration(testEntity.getDuration());
			test.setId(testEntity.getId());
			test.setInstructions(testEntity.getInstructions());
			test.setLogo(testEntity.getLogo());
			test.setName(testEntity.getName());
			test.setNegativeMarks(testEntity.getNegativeMarks());
			test.setPassMarks(testEntity.getPassMarks());
			test.setPositiveMarks(testEntity.getPositiveMarks());
//			test.setQuestions(transformQuestion(testEntity.getQuestions()));
			test.setTestBundle(transformBasicTestBundleDetails(testEntity.getTestBundle()));
			test.setCategories(CategoryMgtBeanTransformer.transformCategory(testEntity.getCategories()));
			test.setTestType(transformTestType(testEntity.getTestType()));
			test.setTimeBased(testEntity.isTimeBased());
			test.setTotalMarks(testEntity.getTotalMarks());			
		}
		
		return test;
	}

	public static TestBundleEntity transformTestBundle(TestBundle testBundle) {
		TestBundleEntity tbe = null;
		
		if (testBundle != null) {
			tbe = new TestBundleEntity();
			tbe.setCode(testBundle.getCode());
			tbe.setDescription(testBundle.getDesc());
			tbe.setName(testBundle.getName());
			tbe.setPublisher(testBundle.getPublisher());
			tbe.setActive(testBundle.isActive());
		}
		
		return tbe;
	}

	public static TestBundle transformTestBundle(TestBundleEntity testBundleEntity) {
		TestBundle testBundle = null;
		
		if (testBundleEntity != null) {
			testBundle = new TestBundle();
			testBundle.setCode(testBundleEntity.getCode());
			testBundle.setDesc(testBundleEntity.getDescription());
			testBundle.setId(testBundleEntity.getId());
			testBundle.setName(testBundleEntity.getName());
			testBundle.setPublisher(testBundleEntity.getPublisher());
			testBundle.setActive(testBundleEntity.isActive());
			
			if (testBundleEntity.getTests() != null && testBundleEntity.getTests().size() > 0) {
				Set<Test> tests = new HashSet<Test>();
				for (TestEntity te : testBundleEntity.getTests()) {
					tests.add(transformTest(te));
				}
				testBundle.setTests(tests);
			}			
		}
		
		return testBundle;
	}

	public static QuestionTypeEntity transformQuestionType(QuestionType questionType) {
		QuestionTypeEntity qte = null;
		
		if (questionType != null) {
			qte = new QuestionTypeEntity();
			qte.setDescription(questionType.getDesc());
			qte.setName(questionType.getName());
		}
		
		return qte;
	}

	public static QuestionType transformQuestionType(QuestionTypeEntity qte) {
		QuestionType qt = null;
		
		if (qte != null) {
			qt = new QuestionType();
			qt.setDesc(qte.getDescription());
			qt.setId(qte.getId());
			qt.setName(qte.getName());			
		}
		
		return qt;
	}

	public static QuestionEntity transformQuestion(Question question) {
		QuestionEntity qe = null;
		
		if (question != null) {
			qe = new QuestionEntity();
			qe.setActive(question.isActive());
			qe.setCode(question.getCode());
			qe.setDescription(question.getDescription());
			qe.setDuration(question.getDuration());
			qe.setNegativeMarks(question.getNegativeMarks());
			qe.setPositiveMarks(question.getPostivieMarks());
			qe.setQuestion(question.getQuestion());
			qe.setSolution(question.getSolution());			
		}
		
		return qe;
	}

	private static Set<OptionEntity> transformOptions(Set<Option> options) {
		Set<OptionEntity> optSet = new HashSet<OptionEntity>();
		OptionEntity optEntity = null;
		for (Option option : options) {
			optEntity = new OptionEntity();
			optEntity.setOpt(option.getOpt());
			
			optSet.add(optEntity);
		}
		
		return optSet;
	}
	
	public static Question transformQuestionWithAnswers(QuestionEntity qe) {
		Question question = null;
		
		if (qe != null) {
			question = new Question();
			question.setActive(qe.isActive());
			question.setAnswers(transformAnswers(qe.getAnswers()));
			question.setCode(qe.getCode());
			question.setDescription(qe.getDescription());
			question.setDuration(qe.getDuration());
			question.setFormatType(transformFormatType(qe.getFormatType()));
			question.setId(qe.getId());
			question.setNegativeMarks(qe.getNegativeMarks());
			question.setOptions(transformOption(qe.getOptions()));
			question.setPostivieMarks(qe.getPositiveMarks());
			question.setQuestion(qe.getQuestion());
			question.setQuestionType(transformQuestionType(qe.getQuestionType()));
			question.setSolution(qe.getSolution());	
		}
		
		return question;
	}
	
	public static Question transformQuestion(QuestionEntity qe) {
		Question question = null;
		
		if (qe != null) {
			question = new Question();
			question.setActive(qe.isActive());
			question.setCode(qe.getCode());
			question.setDescription(qe.getDescription());
			question.setDuration(qe.getDuration());
			question.setFormatType(transformFormatType(qe.getFormatType()));
			question.setId(qe.getId());
			question.setNegativeMarks(qe.getNegativeMarks());
			question.setOptions(transformOption(qe.getOptions()));
			question.setPostivieMarks(qe.getPositiveMarks());
			question.setQuestion(qe.getQuestion());
			question.setQuestionType(transformQuestionType(qe.getQuestionType()));
			question.setSolution(qe.getSolution());			
		}
		
		return question;
	}

	private static Set<Option> transformOptionEntities(Set<OptionEntity> options) {
		Set<Option> optionList = null;
		
		if (options != null && options.size() > 0) {
			optionList = new HashSet<Option>();
			for (OptionEntity oe : options) {
				optionList.add(transformOption(oe));
			}
		}
		
		return optionList;
	}

	private static Set<Option> transformAnswers(Set<AnswersEntity> answers) {
		Set<Option> answerList = null;
		
		if (answers != null && answers.size() > 0) {
			answerList = new HashSet<Option>();
			for(AnswersEntity ae : answers) {
				answerList.add(transformOption(ae.getOption()));
			}
		}
		return answerList;
	}

	public static Set<Question> transformQuestion(Set<QuestionEntity> questionEntities) {
		Set<Question> questionSet = null;
		
		if (questionEntities != null && questionEntities.size() > 0) {
			questionSet = new HashSet<Question>();
			for (QuestionEntity qe : questionEntities) {
				questionSet.add(transformQuestion(qe));
			}
		}
		
		return questionSet;
	}
	
	public static Set<Question> transformQuestionWithAnswers(Set<QuestionEntity> questionEntities) {
		Set<Question> questionSet = null;
		
		if (questionEntities != null && questionEntities.size() > 0) {
			questionSet = new HashSet<Question>();
			for (QuestionEntity qe : questionEntities) {
				questionSet.add(transformQuestionWithAnswers(qe));
			}
		}
		
		return questionSet;
	}

	public static Option transformOption(OptionEntity optionEntity) {
		Option option = null;
		
		if (optionEntity != null) {
			option = new Option();
			option.setFormatType(transformFormatType(optionEntity.getFormatType()));
			option.setId(optionEntity.getId());
			option.setOpt(optionEntity.getOpt());
		}
		
		return option;
	}

	public static List<Test> getTest(List<TestEntity> testEntities) {
		List<Test> testList = null;
		
		if (testEntities != null && testEntities.size() > 0) {
			testList = new ArrayList<Test>();
			for (TestEntity te : testEntities) {
				testList.add(transformTest(te));
			}
		}
		
		return testList;
	}

	public static Set<QuestionEntity> transformQuestions(Set<Question> questions) {
		Set<QuestionEntity> qSet = new HashSet<QuestionEntity>();
		
		for (Question question : questions) {
			qSet.add(transformQuestion(question));
		}
		return qSet;
	}

	public static OptionEntity transformOption(Option option) {
		OptionEntity oe = new OptionEntity();
		oe.setOpt(option.getOpt());
		
		return oe;
	}
	
	public static UserTest transformUserTestDetails(UserTestSubscriptionEntity utse) {
		UserTest ut = new UserTest();
		ut.setGrade(utse.getGrade());
		ut.setId(utse.getId());
		ut.setPass(utse.isPass());
		ut.setPercentage(utse.getPercentage());
		ut.setSubscribedDate(new Date(utse.getSubscribedDate()));
		ut.setPresent(utse.isPresent());
		ut.setTestDate(new Date(utse.getTestDate()));
		ut.setUnsubscribe(utse.isUnsubscribe());		
		
		return ut;
	}

	public static TestEntity transformTest(TestEntity testEntity, Test test) {
		testEntity.setActive(test.isActive());
		testEntity.setAllowChangeAnswers(test.isAllowChangeAnswers());
		testEntity.setAllowReattempt(test.isAllowReattempt());
		testEntity.setAllowReview(test.isAllowReview());
		testEntity.setTimeBased(test.isTimeBased());
		testEntity.setInstructions(test.getInstructions());
		testEntity.setCode(test.getCode());
		testEntity.setCreatedBy(test.getCreatedBy());
		testEntity.setCreatedDate(test.getCreatedDate());
		testEntity.setDescription(test.getDesc());
		testEntity.setName(test.getName());
		testEntity.setNegativeMarks(test.getNegativeMarks());
		testEntity.setPassMarks(test.getPassMarks());
		testEntity.setPositiveMarks(test.getPositiveMarks());
		testEntity.setTotalMarks(test.getTotalMarks());
		testEntity.setDuration(test.getDuration());
		
		return testEntity;
	}

	public static TestTypeEntity transformTestType(TestTypeEntity testTypeEntity, TestType testType) {
		testTypeEntity.setDescription(testType.getDesc());
		testTypeEntity.setName(testType.getName());
		
		return testTypeEntity;
	}

	public static List<TestType> transformTestType(List<TestTypeEntity> tteList) {
		List<TestType> ttList = new ArrayList<TestType>();
		for (TestTypeEntity tte : tteList) {
			ttList.add(transformTestType(tte));			
		}
		
		return ttList;
	}
	
	public static QuestionTypeEntity transformQuestionType(QuestionTypeEntity questionTypeEntity, QuestionType questionType) {
		questionTypeEntity.setDescription(questionType.getDesc());
		questionTypeEntity.setName(questionType.getName());
		
		return questionTypeEntity;
	}

	public static List<QuestionType> transformQuestionType(List<QuestionTypeEntity> qteList) {
		List<QuestionType> qtList = new ArrayList<QuestionType>();
		for (QuestionTypeEntity qte : qteList) {
			qtList.add(transformQuestionType(qte));			
		}
		
		return qtList;
	}
	
	public static QuestionEntity transformQuestion(QuestionEntity qe, Question question) {
		qe.setCode(question.getCode());
		qe.setDescription(question.getDescription());
		qe.setNegativeMarks(question.getNegativeMarks());
		qe.setDuration(question.getDuration());
//		qe.setObjective(question.isObjective());
		
		/*if (question.getOptions() != null && question.getOptions().size() > 0) {
			qe.setOptions(transformOptions(question.getOptions()));
		}*/
		
		qe.setPositiveMarks(question.getPostivieMarks());
		qe.setQuestion(question.getQuestion());
//		qe.setQuestionType(transformQuestionType(question.getQuestionType()));
		
		return qe;
	}

	public static TestBundleEntity transformTestBundle(TestBundleEntity tbe, TestBundle testBundle) {
		tbe.setCode(testBundle.getCode());
		tbe.setDescription(testBundle.getDesc());
		tbe.setName(testBundle.getName());
		tbe.setPublisher(testBundle.getPublisher());
		tbe.setActive(testBundle.isActive());
		
		return tbe;
	}

	public static Set<Option> transformOption(Set<OptionEntity> options) {
		Set<Option> optionSet = null;
		if (options != null && options.size() > 0) {
			optionSet = new HashSet<Option>();
			for (OptionEntity oe : options) {
				optionSet.add(transformOption(oe));
			}
		}
		return optionSet;
	}

	public static FormatType transformFormatType(FormatTypeEntity fte) {
		FormatType formatType = new FormatType();
		
		if (fte != null) {
			formatType.setId(fte.getId());
			formatType.setType(fte.getType());
		}
		return formatType;
	}

	public static FormatTypeEntity transformFormatType(FormatTypeEntity fte, FormatType formatType) {
		fte.setType(formatType.getType());
		
		return fte;
	}

	public static List<FormatType> transformFormatType(List<FormatTypeEntity> fteList) {
		List<FormatType> ftList = new ArrayList<FormatType>();
		for (FormatTypeEntity fte : fteList) {
			ftList.add(transformFormatType(fte));			
		}
		
		return ftList;
	}

	public static List<Test> transformBasicTestDetails(List<TestEntity> testEntities) {
		List<Test> testList = null;
		
		if (testEntities != null && testEntities.size() > 0) {
			testList = new ArrayList<Test>();
			for (TestEntity te : testEntities) {
				testList.add(transformBasicTestDetails(te));
			}
		}
		
		return testList;
	}	

}
